﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DTO;

namespace NGU
{
    public partial class Home : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["ID"] != null)
                return;
            if (!this.IsPostBack && Request.Cookies["remember"] != null)
            {
                HttpCookie cookie = Request.Cookies.Get("unpw");
                if (cookie == null)
                    return;
                if (cookie.Values["un"] != null && cookie.Values["wp"] != null)
                {
                    string username = cookie.Values["un"].ToString();
                    string pwHash = cookie.Values["wp"].ToString();
                    TaiKhoanBUS bus = new TaiKhoanBUS();
                    TaiKhoanDTO tk = bus.layThongTinTaiKhoan(username);
                    if (tk == null)
                    {
                        return;
                    }
                    string realpwHash = bus.getMD5(username + tk.Password);
                    if (pwHash == realpwHash)
                    {
                        Session["ID"] = tk.MaTaiKhoan;
                        Session["Username"] = tk.Username;
                        Session["TrangThai"] = tk.TrangThai;
                        Session.Timeout = 30;
                    }
                }
            }           
        }
    }
}